package com.pai4j.client.service;
import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import com.pai4j.common.exception.BusinessException;
import com.pai4j.common.util.JsonUtil;
import com.pai4j.domain.vo.response.chatting.ChattingListResponseVO;
import com.pai4j.domain.vo.response.mess.ChatMessageResponseVO;
import com.pai4j.remote.ugc.ChatMessageClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;


@Slf4j
@Service
public class ChatMessageService {


    @Autowired
    private ChatMessageClient chatMessageClient;

    public List<ChatMessageResponseVO> list(String currentUserId, String chatUserId, Long lastId, Long fixedMessageId, Integer size) {
        PAIResponseBean<List<ChatMessageResponseVO>> messageResponse = chatMessageClient.list(currentUserId, chatUserId, size, lastId, fixedMessageId);
        if (PAIResponseBeanUtil.isOk(messageResponse)) {
            return messageResponse.getData();
        }
        return Collections.emptyList();
    }

    public List<ChattingListResponseVO> listChatting(String userId, Long lastChatTime, Integer size) {

        // RPC 远程调用，请求 UGC 核心服务获取对话列表数据
        PAIResponseBean<List<ChattingListResponseVO>> responseBean = chatMessageClient.listChatting(userId, lastChatTime, size);
        if (PAIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        log.error("chatMessageClient.listChatting RPC invoke error =====> " +
                "userId:{}, lastChatTime:{}, size:{}, response:{}", userId, lastChatTime, size, JsonUtil.toJsonString(responseBean));
        throw new BusinessException(responseBean.getMessage());
    }
}
